package com.example.final_project.ShoppingCar.frag;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.ejlchina.okhttps.OkHttps;
import com.example.final_project.Internet.Internet;
import com.example.final_project.R;
import com.example.final_project.ShoppingCar.Adapter.ShoppingCarAdapter;
import com.example.final_project.Usr.entry.UserStore;

import java.util.ArrayList;
import java.util.List;

public class ShoppingCarFragment extends Fragment {

    private RecyclerView recyclerView;
    private Button buy;
    private Context context;
    private View view;
    private List<UserStore> userStores = new ArrayList<>(100);
    private int id = 1;
    public static final int STORE = 0;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_shopping_car, container, false);

        Intent intent = this.getActivity().getIntent();
        id = intent.getIntExtra("id",0);
        findById();
        List<UserStore> list = Internet.getInstance().getStoreTag(id);
        getStoreData();
        //立即购买
        buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        return view;
    }

    Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case STORE:
                    List<UserStore> list = (List<UserStore>) msg.obj;
                    UserStore userStore = list.get(0);
                    userStores.add(userStore);
                    ShoppingCarAdapter shoppingCarAdapter = new ShoppingCarAdapter(userStores,context);
                    recyclerView.setAdapter(shoppingCarAdapter);
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context,RecyclerView.VERTICAL,false);
                    recyclerView.setLayoutManager(linearLayoutManager);
                    break;
            }
        }
    };

    //获取网络请求
    private void getStoreData() {
        OkHttps.async("http://10.150.11.9:8086/chengcar/storeid")
                .bind(this)
                .addUrlPara("id", id)
                .setOnResList(UserStore.class, store -> {
                    Message message = Message.obtain();
                    message.what = STORE;
                    message.obj = store;
                    handler.sendMessage(message);
                })
                .get();
    }

    private void findById(){
        recyclerView = view.findViewById(R.id.shoppcar);
        buy = view.findViewById(R.id.shoppcar_buy);
        context = view.getContext();
    }

}